package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.yunxi.core.util.BeanTools;
import com.yunxi.model.sto.StoCarrier;
import com.yunxi.model.sto.StoCarrierBizarea;
import com.yunxi.service.localService.sto.StoCarrierBizareaService;
import com.yunxi.service.localService.sto.StoCarrierService;
import com.yunxi.sto.pojo.StoCarrierBizareaVO;
import com.yunxi.sto.pojo.StoCarrierVO;
import com.yunxi.sto.service.StoCarrierDubboService;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 承运商信息 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-14
 */
public class StoCarrierDubboServiceImpl implements StoCarrierDubboService {

    @Autowired
    @Qualifier(value = "stoCarrierServiceImpl")
    StoCarrierService stoCarrierService;

    @Autowired
    @Qualifier(value = "stoCarrierBizareaServiceImpl")
    StoCarrierBizareaService stoCarrierBizareaService;


    @Override
    public StoCarrierVO getById(String id) {
        StoCarrier stoCarrier = stoCarrierService.getById(id);
        StoCarrierVO stoCarrierVO = new StoCarrierVO();
        BeanTools.copyProperties(stoCarrierVO,stoCarrier);
        return stoCarrierVO;
    }

    @Override
    public void saveOrUpdate(StoCarrierVO stoCarrierVO) throws Exception {
        StoCarrier stoCarrier = new StoCarrier();
        BeanTools.copyProperties(stoCarrier,stoCarrierVO);
        //校验承运商编号不能重复
        checkCodeIsExsit(stoCarrierVO);
        stoCarrierService.saveOrUpdate(stoCarrier);
        List<StoCarrierBizareaVO> bizareaList = stoCarrierVO.getBizareaList();
        if(bizareaList != null && bizareaList.size() > 0){
            for(StoCarrierBizareaVO stoCarrierBizareaVO : bizareaList){
                StoCarrierBizarea stoCarrierBizarea = new StoCarrierBizarea();
                BeanTools.copyProperties(stoCarrierBizarea,stoCarrierBizareaVO);
                stoCarrierBizarea.setCarrierId(stoCarrier.getCarrierId());
                stoCarrierBizareaService.saveOrUpdate(stoCarrierBizarea);
            }
        }
    }

    /**
     * 检查承运商编号是否重复添加
     * @param stoCarrierVO
     * @throws Exception
     */
    private void checkCodeIsExsit(StoCarrierVO stoCarrierVO) throws Exception{
        if(stoCarrierVO.getCarrierCode() != null && StringUtils.isNotEmpty(stoCarrierVO.getCarrierCode())){
            QueryWrapper<StoCarrier> queryWrapper = new QueryWrapper();
            if(stoCarrierVO.getCarrierId() != null && StringUtils.isNotEmpty(stoCarrierVO.getCarrierId())){
                queryWrapper.ne("CARRIER_ID",stoCarrierVO.getCarrierId());
            }
            queryWrapper.eq("CARRIER_CODE",stoCarrierVO.getCarrierCode());
            List<StoCarrier> list = stoCarrierService.list(queryWrapper);
            if(list != null && list.size() > 0){
                throw new Exception("操作失败,承运商编号"+stoCarrierVO.getCarrierCode()+"已存在!");
            }
        }
    }

    @Override
    public void removeById(String id) {
    stoCarrierService.removeById(id);
    }

    @Override
    public void del(List<String> list,Integer isDel,String userId) {
        stoCarrierService.delAndReset(list,isDel,userId);
    }

    @Override
    public void reset(List<String> list,Integer isDel,String userId) {
        stoCarrierService.delAndReset(list,isDel,userId);
    }

    @Override
    public void frozen(List<String> list,Integer isFrozen,String userId) {
        stoCarrierService.frozenAndReset(list,isFrozen,userId);
    }

    @Override
    public void frozenReset(List<String> list,Integer isFrozen,String userId) {
        stoCarrierService.frozenAndReset(list,isFrozen,userId);
    }

    @Override
    public PageInfo<StoCarrierVO> findPageByVO(StoCarrierVO stoCarrierVO, Integer pageNum, Integer pageSize){
        String carriageType1 = stoCarrierVO.getCarriageType();
        if(carriageType1 != null){
            String[] querySplit = carriageType1.split(",");
            List<String> queryCarriageTypeLisy = Arrays.asList(querySplit);
            stoCarrierVO.setCarriageTypeList(queryCarriageTypeLisy);
        }
        PageInfo<StoCarrierVO> pageInfo = stoCarrierService.pageByVo(stoCarrierVO,pageNum,pageSize);
        List<StoCarrierVO> list = pageInfo.getList();
        if(list != null && list.size() > 0){
            for(StoCarrierVO vo : list){
                String carriageType = vo.getCarriageType();
                if(StringUtil.isNotEmpty(carriageType)){
                    String[] split = carriageType.split(",");
                    List<String> strings = Arrays.asList(split);
                    String dictNamesByKey = stoCarrierService.getDictNamesByKey("VehicleShipping_Method",strings);
                    vo.setCarriageTypeName(dictNamesByKey);
                }
            }
        }
        return pageInfo;
   }

}
